$.extend($,{
		"validateRules" : {
				"email" : /^\s*([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+\s*$/,
				"cnphone" : /^\s*(\d{3,4}-?)\d{7,8}(-\d{1,6})?\s*$/,
				"cnmobile" : /^\s*1\d{10}\s*$/,
				"date" : /^[0-9]{3,4}\-[0-9]{1,2}\-[0-9]{1,2}$/,
				"number" : /^\d+$/
		}
});
$.extend($.fn,{
		"validate": function(rules){
				var form = $(this);
				form.submit(function(){
						var result = true;
						$.each(rules, function(name, rule){
								var nodes = form.find("[name='"+name+"']"),
										node = nodes.first(),
										_result = true;
								if(rule.custom){
										if(!rule.custom(nodes)){
												_result = false;
										}
								}else{
										var value = node.val(),
												regexp = rule.regexp;
										if(value == "" && rule.require){
												_result = false;
										}else if(value != ""){
												if(typeof regexp == "string"){
														regexp = jQuery.validateRules[regexp];
												}
												if(!regexp.test(value)) {
														_result = false;
												}
										}
								}
								var td = node.closest("td"),
										box = td.find(".validate-error:first");
								if(_result){
										if(box.length > 0){
												box.remove();
										}
								}else{
										result = false;
										if(box.length == 0){
												td.append("<p class='validate-error'>"+rule.message+"</p>");
										}else{
												box.text(rule.message);
										}
								}
						});
						return result?true:false;
				});
		}
});

$(document).ready(function(){
		var closeBtn = $(".auto-close");
		if(closeBtn.length > 0){
			closeBtn.click(function(e){
					e.preventDefault();
					window.close();
			});
		}
		var addBtn = $(".auto-add");
		if(addBtn.length > 0){
			addBtn.click(function(e){
					e.preventDefault();
					window.open($(this).attr("data"));
			});
		}
		var resetBtn = $(".auto-reset");
		if(resetBtn.length > 0){
			resetBtn.click(function(e){
					e.preventDefault();
					$(this).closest("form").find("input[type='text']").val("");
			});
		}	
		var time = $(".auto-time");
		if(time.length > 0){
				var date = {};
				date.dateFormat = 'yy-mm-dd';
				date.monthNames = ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'];
				date.dayNamesMin = ['日','一','二','三','四','五','六'];

				var dataCongif = {dateFormat:date.dateFormat,monthNames:date.monthNames,dayNamesMin:date.dayNamesMin};
				time.datepicker(dataCongif);
		}
}); 